<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% Option explicit %>

<SCRIPT language="JavaScript" src="/timereport/include/common.js"></SCRIPT>

<SCRIPT language="JavaScript">

function validate() {

// Verifica che tutti i campi obbligatori siano stati compilati
	if ( !ForceEntry(document.form1.hours,"Ore") ) 
		return false;

// Verifica che l'input sia numerico
	if ( !IsNumeric(document.form1.hours) ) 
		return false; 

//	Verifica il formato del campo, al massimo due interi e due decimali		
  	if ( !CheckDecimals(document.form1.hours,2,2) ) 
  		return false;   	
					
	return true;
}
</SCRIPT>


<%

	Dim strDate
	Dim objConn1, objRs1
	Dim strInputDisabled 	' used for disable the input field
	Dim strCssInput		' used for setting the input attribute
	
	strCSSInput = "FormInput" 	' is changed in case of updating not allowed
	
'   --- if user selected the cancel button go back to main screen ---
	if request.form("cancel") <> "" then
		response.redirect("input.asp")
	End If 

'	----------------------------	
'	Values of action:
'	new:	called by input.asp by clicking on "new item" icon
'	fetch:	called by input.asp by clicking on existing item
'	add:	called by itself for insert, insert and add, insert and increment date buttons
'	update: called by itself for update button

	Select case request("action")

	case "fetch"
		call MakeConn(objConn, DATABASE)	
		call MakeRs_view(objRs,objConn,"SELECT * FROM hours  where hours_id =" & request("hours_id") )							
	
	case "add"
		call MakeConn(objConn, DATABASE)
		call MakeRs_add(objRs,objConn,"hours" )	
		objRs.addnew
		objRs("Date")  = request("date")			
		' set values
		objRs("Projects_Id") = request("Projects_id") 			
		objRs("HourType_Id") = request("HourType_Id") 					
		objRs("Persons_Id") = session("persons_id") 
		objRs("Hours") =  iif(request("CancelFlag")="X",-1,1) * request("hours")
		If request("CancelFlag")  = "X" Then 
		    objRs("CancelFlag") = true 
		Else	
		    objRs("CancelFlag") = false
		End If
		If request("TransferFlag")  = "X" Then 
		    objRs("TransferFlag") = true 
		Else	
		    objRs("TransferFlag") = false
		End If
        objRs("comment") = request("comment")						
		objRs.update
		'Clear open connections	
		destroy(objRs)
		destroy(objConn)
		' save the project on a session variable to give a default on the next entry
		session("ProjectCodeDefault") = request("Projects_id") 
		session("HourTypeDefault") = request("HourType_Id") 

	case "update"
		call MakeConn(objConn, DATABASE)	
		call MakeRs_add(objRs,objConn,"SELECT * FROM hours  where hours_id =" & request("hours_id") )							
		' set values
		objRs("Projects_Id") = request("Projects_id") 			
		objRs("HourType_Id") = request("HourType_Id") 					
		objRs("Persons_Id") = session("persons_id") 
		objRs("Hours") =  iif(request("CancelFlag")="X",-1,1) * request("hours") 
		If request("CancelFlag")  = "X" Then 
			objRs("CancelFlag") = true 
		Else	
			objRs("CancelFlag") = false
		End If
		If request("TransferFlag")  = "X" Then 
			objRs("TransferFlag") = true 
		Else	
			objRs("TransferFlag") = false
		End If
        objRs("comment") = request("comment")						
		objRs.update
		'Clear open connections	
		destroy(objRs)
		destroy(objConn)
		' save the project on a session variable to give a default on the next entry
		session("ProjectCodeDefault") = request("Projects_id") 
		session("HourTypeDefault") = request("HourType_Id") 
		' go back to main screen
		response.redirect("input.asp")		
	
	End Select		
		
'	--- If "add another" function was not selected then go back to main screen and it's not the first time
	If request("action") = "add" and request.form("insert+") = "" and request.form("insert+data") = "" Then
		response.redirect("input.asp")		
	End If

'	---------------------------------			
	If request.form("insert+data") <> "" Then	
		strDate = dateadd( "d", 1, request("date"))
	else
		strDate = request("date")
	End if		
	
'------------------------------------------------------
	Function SetDefault( strFieldName )
		
		if request("action") = "fetch" Then ' called in update mode
			if isnumeric (objRs(strFieldName)) Then
				SetDefault =  abs (objRs(strFieldName))
			Else
				SetDefault =  objRs(strFieldName) 
			End if
		Else
			SetDefault = ""
		End If
			
	End Function
%>


<html><!-- InstanceBegin template="/Templates/common.dwt" codeOutsideHTMLIsLocked="false" -->
<!--#include virtual="/timereport/include/auth.asp" -->
<!--#include virtual="/timereport/include/common.asp" -->

<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Input Ore</title>
<!-- InstanceEndEditable --> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<link href="include/commoncss.asp" rel="stylesheet" type="text/css">
<!-- InstanceEndEditable -->
</head>

<SCRIPT language=JavaScript src= "/timereport/include/menu/menu_array.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript src= "/timereport/include/menu/mmenu.js" type=text/javascript></SCRIPT>

<body>
<table width="760" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr> 
    <td height="30" class="BarraTop"><div align="right">Utente: <%= session("UserName") %> 
      </div></td>
  </tr>
  <tr>
    <td height="30">&nbsp;</td>
  </tr>
  <tr> <!-- InstanceBeginEditable name="principale" --> 
    <td >
		<form name="form1" method="post" action="input-ore.asp">
		<table width="60%" border="0" align="center" cellpadding="0" cellspacing="0" class="TabellaForm">
          <tr> 
            <th>Data</th>
            <td> 
<%
	if request("action") = "fetch" Then ' called in update mode
		response.write( FormatDateTime(objRS("date") , 1)  )

		' if input is disabled the set the attribute of the form input and make them disabled		
		if session("CutoffDate") > objRS("date") Then
			strInputDisabled="Disabled"
			strCssInput = "FormInputDisabled"
		else
			strInputDisabled = ""
		End If
				
	else
		response.write(  FormatDateTime(strDate , 1) )
	End If 
%> 
		
	<input type="hidden" name="date"	 value="<%=strDate %>"> 
            </td>
          </tr>
          <tr> 
            <th>Persona</th>
            <td>
<%		
	response.write( Session("UserName") )
%>
            </td>
          </tr>
          <tr> 
            <th width="30">Progetto</th>
            <td>
			<select name="Projects_Id" class="<%=strCSSInput%>" <%=strInputDisabled%> >
<%						
				Dim aProject, i
            	aProject = session("ProgettiForzati")            		
            	
            		For i=0 to Ubound(aProject, 2)
' debug            			response.write(">>" & session("ProjectCodeDefault") & "-" & aProject(0,i) )            			
' debug          			response.write(">>" & ( CInt(session("ProjectCodeDefault")) = aProject(0,i))  )            			
            	
            			if request("action") = "fetch" then	' update record
            				If aProject(0,i) = objRs("Projects_id") Then
            					response.write("<option value=" & aProject(0,i) & " selected >" &aProject(1,i) & " : " & aProject(2,i) & "</option>")            			
            				Else
            					response.write("<option value=" & aProject(0,i) & ">" & aProject(1,i) & " : " & aProject(2,i) & "</option>")            		            					
            				End If
						Else
							If aProject(0,i) = CInt(session("ProjectCodeDefault"))  Then
            					response.write("<option value=" & aProject(0,i) & " selected >" & aProject(1,i) & " : " & aProject(2,i) & "</option>")            		
							Else
            					response.write("<option value=" & aProject(0,i) & ">" & aProject(1,i) & " : " & aProject(2,i) & "</option>")            									
							End if            					
            			End If
            	
            		Next            							
%>
              </select>			  			 
			  </td>
          </tr>
		  <tr> 
            <th width="30">Tipo:</th>
            <td>
			<select name="HourType_Id" class="<%=strCSSInput%>" <%=strInputDisabled%> 
                    unselectable="off" >
<%		
' 				open connection to retrieve project lists
			    call MakeConn(objConn1, DATABASE)
				
            	Call MakeRs_view(objRs1,objConn1, "SELECT HourType_Id, HourTypeCode, Name  FROM HourType WHERE active=" & CTRUE &" ORDER BY HourTypeCode") 
					
				Dim aHourType
            	aHourType = objRs1.getrows            		
            	
            		For i=0 to Ubound(aHourType, 2)            	
           			if request("action") = "fetch" then	' update record
            				If aHourType(0,i) = objRs("HourType_id") Then
	           					response.write("<option value=" & aHourType(0,i) & " selected >" & aHourType(1,i) & " : " & aHourType(2,i) & "</option>")            			
            				Else
            					response.write("<option value=" & aHourType(0,i) & ">" & aHourType(1,i) & " : " & aHourType(2,i) & "</option>")            		            					
            				End If
						Else
							If aHourType(0,i) = CInt(session("HourTypeDefault"))  Then
            					response.write("<option value=" & aHourType(0,i) & " selected >" & aHourType(1,i) & " : " & aHourType(2,i) & "</option>")            		
							Else
            					response.write("<option value=" & aHourType(0,i) & ">" & aHourType(1,i) & " : " & aHourType(2,i) & "</option>")            									
							End if            					
           			End If
            	
            		Next            							
%>
              </select>			  			 
<%              
'	close DB connections  
	destroy(objRs1)          		
	destroy(objConn1)              
%>
			  </td>
          </tr>          
          <tr> 
            <th>Ore</th>
            <td><input name="hours" type="text" class=<%=strCSSInput%> value="<%=SetDefault("hours")%>" <%=strInputDisabled%> ></td>
          </tr>
          </tr>
            <th valign="top">Trasferta</th>
            <td valign="top"><input name="TransferFlag" <%=strInputDisabled%> type="checkbox" value="X" 
<%
	If request("action") = "fetch" Then
		If objRs("TransferFlag") = True Then ' called in update mode
			response.write(" checked ")
		End If
	End If
	%>  ></td>
          </tr>
		  <tr>
            <th valign="top" nowrap>Storno</th>
            <td valign="top"><input name="CancelFlag" <%=strInputDisabled%> type="checkbox" value="X" 
<%
	If request("action") = "fetch" Then
		If objRs("CancelFlag") = True Then ' called in update mode
			response.write(" checked ")
		End If
	End If
	%>  ></td>
	<tr>
          </tr>
           <tr>
            <th valign="top">Nota</th>
            <td valign="top"><textarea name="comment" cols="50" rows="5"  class="<%=strCSSInput%>" <%=strInputDisabled%> ><%=SetDefault("comment")%></textarea></td>
          </tr>

        </table>
      <br>
      <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="50%"> 
            <%	if request("action") = "fetch" then 
					if session("CutoffDate") < objRS("date") Then %> 
            <input type="submit" name="update" value="aggiorna" class="FormButtont" onClick="return validate()">&nbsp;
			<input type="hidden" name="action" value="update">
            <input type="hidden" name="hours_id" value="<%=objRs("hours_id")%>">
              <%	End If 
			  	else %>
              <input type="submit" name="insert" value="salva" class="FormButtont" onClick="return validate()">&nbsp;
  	          <input type="submit" name="insert+" value="salva +ora" class="FormButtont" onClick="return validate()">&nbsp;
			  <input type="submit" name="insert+data" value="salva +data" class="FormButtont" onClick="return validate()">&nbsp;
			  <input type="hidden" name="action" value="add">
            <% End if %>
              <input type="submit" name="cancel" value=" annulla " class="FormButtont"> 
            </td>  
        </tr>
      </table>
	   </form>
	</td>
<%			
	if request("action") = "fetch" Then	' called in update mode
	    destroy(objRs)
		destroy(objConn)				
	End If
%>	
	
    <!-- InstanceEndEditable --></tr>
</table>
</body>
<!-- InstanceEnd --></html>

